package renamer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class EnglishWordNoteProcessor implements FileProcessor {

	@Override
	public void process(File file) {
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			String s;
			int count = 1;
			while ((s = in.readLine()) != null) {
				if (s.matches("^#.*")) {
					sb.append(s + '\n');
				} else {
					if (count != 1)
						sb.append('\n');
					sb.append(count + "." + s + "\n");
					count++;
				}
			}
			in.close();
			// Backup the original note file
			file.renameTo(new File(file.getAbsoluteFile() + ".bak"));

			// Write a new note file with the StringBuilder content
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file)));
			out.write(sb.toString());
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
